package com.hcinfos.gsoo.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.enity.GiftOrder;
import com.hcinfos.gsoo.interfaze.OrderCallback;
import com.lidroid.xutils.BitmapUtils;

public class MyPayAdapter extends BaseAdapter {

	private List<GiftOrder> list;
	private LayoutInflater lif;
	public OrderCallback callback;
	private BitmapUtils bitmapUtils;

	public MyPayAdapter(Context context, List<GiftOrder> list) {

		this.list = list;
		lif = LayoutInflater.from(context);
		bitmapUtils = new BitmapUtils(context);
		bitmapUtils.configDefaultLoadingImage(R.drawable.tc_shop_default_logo);// 默认背景图片
		bitmapUtils
				.configDefaultLoadFailedImage(R.drawable.tc_shop_default_logo);// 加载失败图片
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = lif.inflate(R.layout.adapter_my_pay_list_item, null);

			convertView.setTag(holder);

			holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.money_tv = (TextView) convertView
					.findViewById(R.id.money_tv);
			holder.inter_tv = (TextView) convertView
					.findViewById(R.id.inter_tv);
			holder.pay_state_tv = (TextView) convertView
					.findViewById(R.id.pay_state_tv);
			holder.ok_btn = (Button) convertView.findViewById(R.id.ok_btn);
			holder.pay_btn = (Button) convertView.findViewById(R.id.pay_btn);
			holder.yun_tv = (TextView) convertView.findViewById(R.id.yun_tv);
			holder.rec_tv = (TextView) convertView.findViewById(R.id.rec_tv);
			holder.address_tv = (TextView) convertView
					.findViewById(R.id.address_tv);
		} else {
			holder = (Holder) convertView.getTag();

		}

		final GiftOrder order = list.get(position);
		holder.time_tv.setText("下单时间：" + order.getUpdateDatetime());
		holder.name_tv.setText(order.getGiftName());
		holder.money_tv.setText("金额: " + "￥" + order.getGiftPrice());
		holder.inter_tv.setText(order.getIntegrate() + "");
		holder.yun_tv.setText("邮费: " + "￥" + order.getExpressFee());
		int state = order.getState();
		switch (state) {
		case 0:
			holder.pay_state_tv.setText("待发送");
			holder.pay_state_tv.setVisibility(View.VISIBLE);

			holder.pay_btn.setVisibility(View.GONE);
			holder.ok_btn.setVisibility(View.GONE);
			break;
		case 1:
			holder.pay_state_tv.setText("发送中");
			holder.pay_state_tv.setVisibility(View.VISIBLE);

			holder.pay_btn.setVisibility(View.GONE);
			holder.ok_btn.setVisibility(View.GONE);
			break;
		case 2:
			holder.pay_state_tv.setText("已完成");
			holder.pay_state_tv.setVisibility(View.VISIBLE);
			holder.pay_btn.setVisibility(View.GONE);
			holder.ok_btn.setVisibility(View.GONE);
			break;
		case 3:
			holder.pay_state_tv.setText("未支付");
			holder.pay_state_tv.setVisibility(View.GONE);
			holder.pay_btn.setVisibility(View.VISIBLE);
			holder.ok_btn.setVisibility(View.GONE);
			break;
		case 4:
			holder.pay_state_tv.setText("待确认");
			holder.pay_btn.setVisibility(View.GONE);
			holder.ok_btn.setVisibility(View.VISIBLE);
			break;

		}

		holder.pay_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (callback != null)
					callback.callback(0,
							order.getId() + "," + order.getGiftName() + ","
									+ order.getExpressFee());

			}
		});
		holder.ok_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (callback != null)
					callback.callback(1, order.getId());
			}
		});

		holder.rec_tv.setText("联系人:" + order.getContactName() + "    "
				+ order.getContact());
		holder.address_tv.setText("地址：" + order.getDiliverAddr());

		bitmapUtils.display(holder.icon_iv, order.getGiftPicUrl());
		return convertView;
	}

	public final class Holder {

		TextView time_tv;
		ImageView icon_iv;
		TextView name_tv;
		TextView money_tv;
		TextView inter_tv;
		TextView pay_state_tv;
		Button pay_btn;
		Button ok_btn;
		TextView yun_tv;
		TextView rec_tv;
		TextView address_tv;

	}

	public void setDataChange(List<GiftOrder> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void setCallback(OrderCallback call) {
		this.callback = call;
	}
}
